package com.bharat.cm.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.bharat.cm.domain.Student;

@Controller
@RequestMapping(value = "/admission")
public class StudentAdmissionFormController {

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView prepareStudentAdmissionForm(){
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("stu", new Student());
		mav.setViewName("studentAdmissionForm");
		return mav;
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView processStudentAdmissionForm(@ModelAttribute("stu") Student student,
			Model model, 
			BindingResult bindingResult){
		
		ModelAndView mav = new ModelAndView();
		if(bindingResult.hasErrors()) {
			mav.setViewName("studentAdmissionForm");
			return mav;
		}
		// TODO: call service method to create student's record
		mav.setViewName("redirect:/admissionSuccess");
		return mav;
	}
}
